
// 返回年月日
function returnTime(time, _str, s) {
	var mydate = new Date(time);
	var year = mydate.getFullYear();
	var month = mydate.getMonth() + 1;
	var day = mydate.getDate();
	var week = mydate.getDay();
	var hour = mydate.getHours();
	var minute = mydate.getMinutes();
	var second = mydate.getSeconds();
	//		  return { year: year, month: month, day: day, week: week};
	var endTime = year + _str + (month > 9 ? month : "0" + month) + _str + (day > 9 ? day : "0" + day);

	//判断是否返小时
	if (s == "hour") {
		endTime = endTime + " " + (hour < 9 ? ("0") + hour : hour) + ":" + (minute < 9 ? ("0") + minute : minute)
	}

	//判断是否返回秒
	if (s == 's') {
		endTime = endTime + " " + (hour < 9 ? ("0") + hour : hour) + ":" + (minute < 9 ? ("0") + minute : minute) + ":" + (second < 9 ? ("0") + second : second);
	}

	return endTime;
}

// 验证手机号码是否正确
function returnPhone(vle) {
	var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/; //手机号码
	if (myreg.test(vle)) {
		return 1;
	} else {
		return 0;
	}
}


// 定义一个深拷贝函数  接收目标target参数
function deepClone(target) {
	// 定义一个变量
	let result;
	// 如果当前需要深拷贝的是一个对象的话
	if (typeof target === 'object') {
	  // 如果是一个数组的话
	  if (Array.isArray(target)) {
		result = []; // 将result赋值为一个数组，并且执行遍历
		for (let i in target) {
		  // 递归克隆数组中的每一项
		  result.push(this.deepClone(target[i]))
		}
		// 判断如果当前的值是null的话；直接赋值为null
	  } else if (target === null) {
		result = null;
		// 判断如果当前的值是一个RegExp对象的话，直接赋值    
	  } else if (target.constructor === RegExp) {
		result = target;
	  } else {
		// 否则是普通对象，直接for in循环，递归赋值对象的所有值
		result = {};
		for (let i in target) {
		  result[i] = this.deepClone(target[i]);
		}
	  }
	  // 如果不是对象的话，就是基本数据类型，那么直接赋值
	} else {
	  result = target;
	}
	// 返回最终结果
	return result;
  }


//删除提示
function delTips(fn) {
	return function(){
		let args = arguments; // arguments中存着
		this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		}).then(() => {
			if (typeof fn == "function") {
				fn.apply(this,args)//将this和参数传给原函数

			}
		}).catch(() => {
			this.$message({
				type: 'info',
				message: '已取消删除'
			});
		});
	}
	
}

// 删除提示
function delTs(that) {
	return new Promise((resolve, reject) => {
		that.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning'
		}).then(() => {
			resolve()
		}).catch(() => {
			that.$message({
				type: 'info',
				message: '已取消删除'
			});
		});
	})
}

//判断是否是json
function isJSON(str) {
	if (typeof str == 'string') {
		try {
			JSON.parse(str);
			return true;
		} catch (e) {
			return false;
		}
	}
}


// 身份证验证
function checkIdCard(idcard) {
	const regIdCard = /(^\d{15}$)|(^\d{17}([0-9]|X|x)$)/;
	if (!regIdCard.test(idcard)) {
		return false;
	} else {
		return true;
	}
}


//文件上传进度条
function uploadSectionFile(form, successfn, percentFn) {
	var request = new XMLHttpRequest();
	request.open("POST", "https://afe.aapppp.cn/admin/video");
	request.onload = function (e) {
		if (e.currentTarget.status == 200) {
			var data = JSON.parse(request.responseText);
			successfn(data);

		}
	}
	//【上传进度调用方法实现】
	request.upload.onprogress = function (evt) {
		if (evt.lengthComputable) {
			var numbers = Math.round(evt.loaded / evt.total * 100);
			percentFn(numbers)
		}
	};
	request.send(form);
}



// 相差时间
function countDay(start, end) {
	var date1 = new Date(start);
	var date2 = new Date(end);
	var date = (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24);/*不用考虑闰年否*/
	return date;
}

//小于10自动补0
function add0(m){
	return m<10?'0'+m:m 
}

//时间戳转日期格式    //2020-12-16 14:36:35
function format(shijianchuo)
{
	//shijianchuo是整数，否则要parseInt转换
	var time = new Date(shijianchuo);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm);
};
 
function base64DataToBlob(base64Url,fileName){ 
  ////将图片Base64 转成文件
  var arr = base64Url.split(','),
      mime = arr[0].match(/:(.*?);/)[1],
      str = atob(arr[1]),
      n = str.length,
      u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = str.charCodeAt(n);
  }
  return new File([u8arr], fileName, { type: mime });
}

//日期格式转化中国标准时间
function formatterDate (date) {
	let result = new Date(date);
	return result;
};


function clickMarker(res) {
	console.log(res, '多少')
}


module.exports = {
  base64DataToBlob,
	returnTime,
	isJSON,
	returnPhone,
	delTips,
	delTs,
	checkIdCard,
	uploadSectionFile,
	countDay,
	clickMarker,
	deepClone,
	format,
	formatterDate

}